home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / common / asynchttp / handlers.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  7KB  |  171 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import logging
  5. import urlparse
  6. import urllib
  7. import urllib2
  8. log = logging.getLogger('asynchttp.handlers')
  9. __all__ = [
  10.     'AsyncHTTPBasicAuthHandler',
  11.     'AsyncHTTPDigestAuthHandler',
  12.     'AsyncHTTPRedirectHandler',
  13.     'AsyncProxyBasicAuthHandler',
  14.     'AsyncProxyDigestAuthHandler',
  15.     'AsyncProxyHandler']
  16. redirect_codes = frozenset((301, 302, 303, 307))
  17.  
  18. class AsyncHTTPRedirectHandler(urllib2.HTTPRedirectHandler):
  19.     max_repeats = 4
  20.     max_redirections = 10
  21.     
  22.     def http_response(self, request, response):
  23.         if response.code not in redirect_codes or not (request.follow_redirects):
  24.             return None
  25.         
  26.         headers = response.headers
  27.         newurl = headers.get('location', headers.get('uri', None))
  28.         if newurl is None:
  29.             return None
  30.         
  31.         newurl = urlparse.urljoin(request.get_full_url(), newurl).replace(' ', '%20')
  32.         new_request = request.copy(url = newurl, unverifiable = True)
  33.         del new_request.headers['Host']
  34.         if hasattr(request, 'redirect_dict'):
  35.             visited = new_request.redirect_dict = request.redirect_dict
  36.             if visited.get(newurl, 0) >= self.max_repeats or len(visited) >= self.max_redirections:
  37.                 return None
  38.             
  39.         else:
  40.             visited = new_request.redirect_dict = request.redirect_dict = { }
  41.         visited[newurl] = visited.get(newurl, 0) + 1
  42.         log.info('redirect: %r -> %r', request.get_full_url(), new_request.get_full_url())
  43.         return ('request', new_request)
  44.  
  45.  
  46.  
  47. class AsyncProxyHandler(urllib2.ProxyHandler):
  48.     
  49.     def http_request(self, request):
  50.         
  51.         try:
  52.             proxy = self.proxies['http']
  53.         except KeyError:
  54.             return None
  55.  
  56.         (proxy_type, user, password, hostport) = urllib2._parse_proxy(proxy)
  57.         if proxy_type is None:
  58.             proxy_type = 'http'
  59.         
  60.         if user and password:
  61.             user_pass = '%s:%s' % (urllib2.unquote(user), urllib2.unquote(password))
  62.             creds = user_pass.encode('base64').strip()
  63.             request.headers['Proxy-Authorization'] = 'Basic ' + creds
  64.         
  65.         if proxy_type != 'http':
  66.             log.warning('Got unexpected proxy type %r in asynchttp', proxy_type)
  67.             proxy_type = 'http'
  68.         
  69.         hostport = urllib.unquote(hostport)
  70.         request.set_proxy(hostport, proxy_type)
  71.  
  72.  
  73.  
  74. class AsyncAbstractBasicAuthHandler(urllib2.AbstractBasicAuthHandler):
  75.     
  76.     def http_error_auth_reqed(self, req, resp, host, authheader):
  77.         authheader = resp.headers.get(authheader, None)
  78.         if authheader:
  79.             mo = urllib2.AbstractBasicAuthHandler.rx.search(authheader)
  80.             if mo:
  81.                 (scheme, realm) = mo.groups()
  82.                 if scheme.lower() == 'basic':
  83.                     return self.retry_http_basic_auth(req, resp, host, realm)
  84.                 
  85.             
  86.         
  87.  
  88.     
  89.     def retry_http_basic_auth(self, req, resp, host, realm):
  90.         (user, password) = self.passwd.find_user_password(realm, host)
  91.         if password is not None:
  92.             raw = '%s:%s' % (user, password)
  93.             auth = 'Basic %s' % raw.encode('base64').strip()
  94.             if req.headers.get(self.auth_header, None) == auth:
  95.                 return None
  96.             
  97.             req.add_header(self.auth_header, auth)
  98.             return ('request', req)
  99.         
  100.  
  101.  
  102.  
  103. class AsyncHTTPBasicAuthHandler(AsyncAbstractBasicAuthHandler, urllib2.BaseHandler):
  104.     auth_header = 'Authorization'
  105.     
  106.     def http_response_401(self, req, resp):
  107.         url = req.get_full_url()
  108.         return self.http_error_auth_reqed(req, resp, url, 'www-authenticate')
  109.  
  110.  
  111.  
  112. class AsyncProxyBasicAuthHandler(AsyncAbstractBasicAuthHandler, urllib2.BaseHandler):
  113.     auth_header = 'Proxy-authorization'
  114.     
  115.     def http_response_407(self, req, resp):
  116.         authority = req.get_host()
  117.         return self.http_error_auth_reqed(req, resp, authority, 'proxy-authenticate')
  118.  
  119.  
  120.  
  121. class AsyncAbstractDigestAuthHandler(urllib2.AbstractDigestAuthHandler):
  122.     
  123.     def http_error_auth_reqed(self, req, resp, host, authheader):
  124.         authreq = req.headers.get(authheader, None)
  125.         if authreq:
  126.             scheme = authreq.split()[0]
  127.             if scheme.lower() == 'digest':
  128.                 return self.retry_http_digest_auth(req, resp, host, authreq)
  129.             
  130.         
  131.  
  132.     
  133.     def retry_http_digest_auth(self, req, resp, host, auth):
  134.         (_token, challenge) = auth.split(' ', 1)
  135.         chal = urllib2.parse_keqv_list(urllib2.parse_http_list(challenge))
  136.         auth = self.get_authorization(req, chal)
  137.         if auth:
  138.             auth_val = 'Digest %s' % auth
  139.             if req.headers.get(self.auth_header, None) == auth_val:
  140.                 return None
  141.             
  142.             req.add_unredirected_header(self.auth_header, auth_val)
  143.             return ('request', req)
  144.         
  145.  
  146.  
  147.  
  148. class AsyncHTTPDigestAuthHandler(AsyncAbstractDigestAuthHandler, urllib2.BaseHandler):
  149.     auth_header = 'Authorization'
  150.     handler_order = 490
  151.     
  152.     def http_response_401(self, req, resp):
  153.         host = urlparse.urlparse(req.get_full_url())[1]
  154.         val = self.http_error_auth_reqed(req, resp, host, 'www-authenticate')
  155.         self.reset_retry_count()
  156.         return val
  157.  
  158.  
  159.  
  160. class AsyncProxyDigestAuthHandler(AsyncAbstractDigestAuthHandler, urllib2.BaseHandler):
  161.     auth_header = 'Proxy-Authorization'
  162.     handler_order = 490
  163.     
  164.     def http_response_407(self, req, resp):
  165.         host = req.get_host()
  166.         val = self.http_error_auth_reqed(req, resp, host, 'proxy-authenticate')
  167.         self.reset_retry_count()
  168.         return val
  169.  
  170.  
  171.